# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=python2
pkgver=2.7.17
pkgrel=1
_pybasever=2.7
pkgdesc="A high-level scripting language"
arch=('i686' 'x86_64')
license=('PSF')
url="https://www.python.org/"
depends=('gdbm' 'libbz2' 'libopenssl' 'zlib' 'libexpat' 'libsqlite' 'libffi' 'ncurses' 'libreadline')
makedepends=('libbz2-devel' 'openssl-devel' 'zlib-devel' 'libexpat-devel' 'libgdbm-devel' 'libsqlite-devel' 'libffi-devel' 'ncurses-devel' 'libreadline-devel')
#optdepends=('tk: for IDLE')
conflicts=('python<3')
options=('!makeflags') # '!strip' 'debug')
source=(https://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz
        0010-ctypes-util-find_library.patch
        0020-tkinter-x11.patch
        0040-FD_SETSIZE.patch
        0050-export-PySignal_SetWakeupFd.patch
        0060-ncurses-abi6.patch
        0070-dbm.patch
        0080-dylib.patch
        0090-getpath-exe-extension.patch
        0091-2.7.13-ftm.patch
        0100-no-libm.patch
        0200-msys2.patch
        0210-reorder-bininstall-ln-symlink-creation.patch
        0250-allow-win-drives-in-os-path-isabs.patch
        0260-fix-sys-prefix-when-win-drive-in-argv0.patch)
sha256sums=('4d43f033cdbd0aa7b7023c81b0e986fd11e653b5248dac9144d508f11812ba41'
            'e86e0b379e4bfe99b1315979535f4b5036a65caff9e70514e0bc00556580e8e5'
            '6ec74b99877088a108b762de4fadedcc16ab75edeb80caebd294ac2ea665cb58'
            '7522f628554780f6fa6ce7fb111c2b588e34bb4a0789e310a60ca5d8adf6d3d5'
            '86f67a10a8543e5afbf2664858913f51d43bf66370aab794accb806401c7b1ac'
            'a9aea9162ab3364e56be77d71e79952997bb2764a959f2b6a124114e12ef1cc1'
            'bb40ed36f22969f7a7ac2caac8baaada2f244e23167e7b74544198b96aa9ca75'
            '69cdcb55b2d01c97819b1647c63263e5dcff9cba79ef8fae8e704d77c8ec4e4b'
            '11d3c071f35af9ab61a78b943b57ec62f447213c95005663c0a703be9cc65d63'
            'a8d825bbe7efa021aa0b6a8569a7b39f4f8fdcacdd1dca28091ae0691afd137f'
            'd502dc1264960b97be268bfa94d3ce0f1736813dd4351035a396fdf92fe54a96'
            '41939c85350b4f677a3d819f9962714c8a8679e604b29354126fdc48944c232d'
            '6b3a10ad0e00148073b0a7213b3ad84e8da8461429347b2c2acb84444c003874'
            '555949be83449ff94d9f70a41d6b526f5861edcd97e2a8375394e44b75524776'
            '1bf60a68e8c424a4f60cd8c6e7e0d5adbe276fb1a0ce1a35466384aab7476f06')

prepare() {
  cd "${srcdir}/Python-${pkgver}"

  patch -p1 -i ${srcdir}/0010-ctypes-util-find_library.patch
  patch -p1 -i ${srcdir}/0020-tkinter-x11.patch
  patch -p1 -i ${srcdir}/0040-FD_SETSIZE.patch
  patch -p1 -i ${srcdir}/0050-export-PySignal_SetWakeupFd.patch
  patch -p1 -i ${srcdir}/0060-ncurses-abi6.patch
  patch -p1 -i ${srcdir}/0070-dbm.patch
  patch -p1 -i ${srcdir}/0080-dylib.patch
  patch -p1 -i ${srcdir}/0090-getpath-exe-extension.patch
  patch -p1 -i ${srcdir}/0091-2.7.13-ftm.patch
  patch -p1 -i ${srcdir}/0100-no-libm.patch
  patch -p1 -i ${srcdir}/0200-msys2.patch
  patch -p1 -i ${srcdir}/0210-reorder-bininstall-ln-symlink-creation.patch
  patch -p1 -i ${srcdir}/0250-allow-win-drives-in-os-path-isabs.patch
  patch -p1 -i ${srcdir}/0260-fix-sys-prefix-when-win-drive-in-argv0.patch

  # Temporary workaround for FS#22322
  # See https://bugs.python.org/issue10835 for upstream report
  sed -i "/progname =/s/python/python${_pybasever}/" Python/pythonrun.c

  # Enable built-in SQLite module to load extensions (fix FS#22122)
  sed -i "/SQLITE_OMIT_LOAD_EXTENSION/d" setup.py

  # FS#23997
  sed -i -e "s|^#.* /usr/local/bin/python|#!/usr/bin/python2|" Lib/cgi.py

  sed -i "s/python2.3/python2/g" Lib/distutils/tests/test_build_scripts.py \
     Lib/distutils/tests/test_install_scripts.py Tools/scripts/gprof2html.py

  # Ensure that we are using the system copy of various libraries (expat, zlib and libffi),
  # rather than copies shipped in the tarball
  rm -r Modules/expat
  rm -r Modules/zlib
  rm -r Modules/_ctypes/{darwin,libffi}*
}

check() {
  cd "${srcdir}/Python-${pkgver}"
  make test
}

build() {
  cd "${srcdir}/Python-${pkgver}"

  if check_option "strip" "y"; then
    LDFLAGS+=" -s "
  fi
  if check_option "debug" "n"; then
    CFLAGS+=" -DNDEBUG "
    CXXFLAGS+=" -DNDEBUG "
  else
    plain " -DDEBUG -DPy_DEBUG -D_DEBUG does not work unfortunately .."
    #    CFLAGS+=" -DDEBUG -DPy_DEBUG -D_DEBUG "
    #    CXXFLAGS+=" -DDEBUG -DPy_DEBUG -D_DEBUG "
    CFLAGS+=" -O0 -ggdb"
    CXXFLAGS+=" -O0 -ggdb"
  fi

  export OPT="${CFLAGS}"
  ./configure --build=${CHOST} \
    --prefix=/usr \
    --enable-shared \
    --enable-ipv6 \
    --with-libc= \
    --with-libm= \
    --with-system-expat \
    --with-system-ffi \
    --with-threads \
    ac_cv_func_bind_textdomain_codeset=yes \
    CPPFLAGS="${CPPFLAGS} -I/usr/include/ncursesw" \
    LDFLAGS="${LDFLAGS} -L."

  make
}

package() {
  cd "${srcdir}/Python-${pkgver}"
  make DESTDIR="${pkgdir}" altinstall maninstall

  rm "${pkgdir}"/usr/share/man/man1/python.1

  ln -sf python${_pybasever}        "${pkgdir}"/usr/bin/python2.exe
  ln -sf python${_pybasever}-config "${pkgdir}"/usr/bin/python2-config
  ln -sf python${_pybasever}.1      "${pkgdir}"/usr/share/man/man1/python2.1

  # FS#33954
  ln -sf python-${_pybasever}.pc    "${pkgdir}"/usr/lib/pkgconfig/python2.pc

  cp -f "${pkgdir}"/usr/lib/python${_pybasever}/config/libpython${_pybasever}.dll.a "${pkgdir}"/usr/lib/libpython${_pybasever}.dll.a

  mv "${pkgdir}"/usr/bin/smtpd.py "${pkgdir}"/usr/lib/python${_pybasever}/

  # some useful "stuff"
  install -dm755 "${pkgdir}"/usr/lib/python${_pybasever}/Tools/{i18n,scripts}
  install -m755 Tools/i18n/{msgfmt,pygettext}.py "${pkgdir}"/usr/lib/python${_pybasever}/Tools/i18n/
  install -m755 Tools/scripts/{README,*py} "${pkgdir}"/usr/lib/python${_pybasever}/Tools/scripts/

  # fix conflicts with python
  mv "${pkgdir}"/usr/bin/idle{,2}
  mv "${pkgdir}"/usr/bin/pydoc{,2}
  mv "${pkgdir}"/usr/bin/2to3{,-2.7}

  # clean up #!s
  find "${pkgdir}"/usr/lib/python${_pybasever}/ -name '*.py' | \
    xargs sed -i "s|#[ ]*![ ]*/usr/bin/env python$|#!/usr/bin/env python2|"

  # clean-up reference to build directory
  sed -i "s#${srcdir}/Python-${pkgver}:##" "${pkgdir}"/usr/lib/python${_pybasever}/config/Makefile

  # license
  install -Dm644 LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE

  # fix permissons
  find ${pkgdir}/usr -type f \( -name "*.dll" -o -name "*.exe"  \) | xargs chmod 0755
}
